var _baseComponent = _interopRequireDefault(require("../helpers/baseComponent")), _classNames2 = _interopRequireDefault(require("../helpers/classNames")), _eventsMixin = _interopRequireDefault(require("../helpers/eventsMixin"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _toConsumableArray(e) {
    return _arrayWithoutHoles(e) || _iterableToArray(e) || _nonIterableSpread();
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance");
}

function _iterableToArray(e) {
    if (Symbol.iterator in Object(e) || "[object Arguments]" === Object.prototype.toString.call(e)) return Array.from(e);
}

function _arrayWithoutHoles(e) {
    if (Array.isArray(e)) {
        for (var t = 0, a = new Array(e.length); t < e.length; t++) a[t] = e[t];
        return a;
    }
}

function _defineProperty(e, t, a) {
    return t in e ? Object.defineProperty(e, t, {
        value: a,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[t] = a, e;
}

(0, _baseComponent.default)({
    behaviors: [ (0, _eventsMixin.default)() ],
    relations: {
        "../field/index": {
            type: "ancestor"
        }
    },
    properties: {
        prefixCls: {
            type: String,
            value: "wux-rater"
        },
        max: {
            type: Number,
            value: 5,
            observer: function() {
                this.setValue(this.data.inputValue);
            }
        },
        icon: {
            type: String,
            value: ""
        },
        star: {
            type: String,
            value: "★"
        },
        defaultValue: {
            type: Number,
            value: 0
        },
        value: {
            type: Number,
            value: 0,
            observer: function(e) {
                this.data.controlled && this.setValue(e);
            }
        },
        activeColor: {
            type: String,
            value: "#ffc900"
        },
        margin: {
            type: Number,
            value: 2
        },
        fontSize: {
            type: Number,
            value: 25
        },
        disabled: {
            type: Boolean,
            value: !1
        },
        allowHalf: {
            type: Boolean,
            value: !1
        },
        allowClear: {
            type: Boolean,
            value: !1
        },
        allowTouchMove: {
            type: Boolean,
            value: !1
        },
        controlled: {
            type: Boolean,
            value: !1
        }
    },
    data: {
        inputValue: 0
    },
    computed: {
        classes: [ "prefixCls, disabled", function(e, t) {
            return {
                wrap: (0, _classNames2.default)(e, _defineProperty({}, "".concat(e, "--disabled"), t)),
                star: "".concat(e, "__star"),
                box: "".concat(e, "__box"),
                inner: "".concat(e, "__inner"),
                outer: "".concat(e, "__outer"),
                icon: "".concat(e, "__icon")
            };
        } ]
    },
    observers: _defineProperty({}, "inputValue, max, activeColor", function(n, e, r) {
        var t = _toConsumableArray(new Array(e)).map(function(e, t) {
            return t;
        }), a = t.reduce(function(e, t, a) {
            return [].concat(_toConsumableArray(e), [ a <= n - 1 ? r : "#ccc" ]);
        }, []), e = n.toString().split("."), e = 1 === e.length ? [ e[0], 0 ] : e;
        this.setData({
            stars: t,
            colors: a,
            cutIndex: +e[0],
            cutPercent: 10 * e[1]
        });
    }),
    methods: {
        updated: function(e) {
            this.hasFieldDecorator || this.data.inputValue !== e && this.setData({
                inputValue: e
            });
        },
        setValue: function(e) {
            var t = this.data.max, e = e <= 0 ? 0 : t < e ? t : e;
            this.updated(e);
        },
        updateHalfStarValue: function(a, n, r) {
            var o = this, e = this.data.prefixCls;
            wx.createSelectorQuery().in(this).selectAll(".".concat(e, "__star")).boundingClientRect(function(e) {
                var t;
                e.filter(function(e) {
                    return !e;
                }).length || (e = (t = e[a]).left, t = t.width, t = n - e < t / 2 ? a + .5 : a + 1, 
                r.call(o, t, a));
            }).exec();
        },
        onTap: function(e) {
            var n = this, t = e.currentTarget.dataset.index, a = this.data, r = a.inputValue, o = a.disabled, i = a.allowHalf, l = a.allowClear;
            o || (i ? this.updateHalfStarValue(t, e.detail.x, function(e, t) {
                var a = l && e === r;
                n.onChange(a ? 0 : e, t);
            }) : (i = t + 1, e = l && i === r, this.onChange(e ? 0 : i, t)));
        },
        onChange: function(e, t) {
            this.data.controlled || this.setValue(e), this.triggerEvent("change", {
                value: e,
                index: t
            });
        },
        onTouchMove: function(e) {
            var r, o = this, t = this.data, a = t.disabled, i = t.allowHalf, t = t.allowTouchMove;
            !a && t && (r = e.changedTouches[0].pageX, e = this.data.prefixCls, wx.createSelectorQuery().in(this).selectAll(".".concat(e, "__star")).boundingClientRect(function(e) {
                var t, a, n;
                e.filter(function(e) {
                    return !e;
                }).length || (t = (a = e[0]).left, n = a.width, a = e.map(function(e) {
                    return e.width;
                }).reduce(function(e, t) {
                    return e + t;
                }), t = r - t, n = Math.ceil(t / n), 0 < t && t < a && (a = n - 1, i && (e = e[a], 
                n = r - e.left < e.width / 2 ? n - .5 : n), o.onChange(n, a)));
            }).exec());
        }
    },
    attached: function() {
        var e = this.data, t = e.defaultValue, a = e.value, t = e.controlled ? a : t;
        this.setValue(t);
    }
});